<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文章列表</title>
    <link rel="shortcut icon" href="/img/favicon.ico"/>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/common/layuimini/layui.css" media="all">
    <link rel="stylesheet" href="/common/layuimini/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
                <fieldset class="table-search-fieldset">
                    <legend>搜索信息</legend>
                    <div style="margin: 10px 10px 10px 10px">
                        <div class="layui-form layui-form-pane">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">文章标题</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="articleTitle" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">文章类别</label>
                                    <div class="layui-input-inline">
                                        <div class="articleCategorySelect" id="articleCategorySelect"></div>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">作者</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="articleAuthor" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button id="queryArticle" type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </fieldset>
        <table id="articleTable" lay-filter="articleTable"></table>
        <script type="text/html" id="toolBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<!--<h1>文章列表</h1>-->
<!--<button id="windowOpen">确定</button>-->
<!--<a href="/getDemo">跳转</a>-->
</body>
<script type="text/html" id="currentTableBar">
    <div class="layui-btn-container">
<!--        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>-->
        <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
<!--        <button class="layui-btn layui-btn-sm layui-btn-primary layui-border-green " lay-event="import"> 导入 </button>-->
    </div>
</script>
<script type="text/javascript" src="/common/layui/layui.js"></script>
<script type="text/javascript" src="/common/jquery-1.11.3.js"></script>
<script type="text/javascript" src="/common/xm-select/xm-select.js"></script>
<script type="text/javascript">

    //多选下拉框
    var demo1 = xmSelect.render({
        el: '#articleCategorySelect',
        radio: true,
        clickClose: true, //点击之后关闭
        theme: {
            color: '#0081ff',
        },
        data: getCategoryData()
    })

    /**
     * 获取博客分类下拉框数据
     */
    function getCategoryData(){
        var catagoryData;
        $.ajax({
            url: "/category/queryAllCategory",
            type: "post",
            async:false,//取消异步请求
            success: function (res){
                var data = res.data;
                var dataValue = new Array();
                for (var i = 0; i < data.length; i++) {
                    var array = data[i];
                    var obj = new Object();
                    obj.name = array.categoryName;
                    obj.value = array.id;
                    dataValue.push(obj);
                }
                catagoryData = dataValue;
            }
        })
        return catagoryData;
    }




    layui.use(['jquery','form','table'],function (){

        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            util = layui.util;

        //表格实例化
        table.render({
            elem: "#articleTable",
            url: "/article/table",
            toolbar: "#currentTableBar",
            id: "currentArticleTable",
            cols: [
                [
                    {type: 'checkbox'},
                    {field: '', title: '序号',width:60,templet:function (d){
                            return d.LAY_INDEX;
                    }},
                    {field: 'title', title: '标题',event:'getArticleDetail',width:300,style:'color: #2D93CA'},
                    {field: 'authorName', title: '作者', width: 150},
                    {field: 'issued', title: '状态', width: 100,templet(d){

                        if (d.issued === 1){
                            return '已发布';
                        }else if (d.issued === 2){
                            return '草稿箱';
                        }
                        }},
                    {field: 'categoryName', title: '分类', width: 170,templet(d){
                        var str = '';
                        if (d.categoryName === '') {
                            str = '<span class="layui-badge layui-bg-black">-</span>';
                        } else {
                            var arr = d.categoryName.split(',');
                            str = '';
                            for (var i = 0; i < arr.length; i++) {
                                str = str + '<span class="layui-badge layui-bg-black">' + arr[i] + '</span>&nbsp;';
                            }
                        }
                        return str;
                    }},
                    {field: 'createTime', title: '创建时间', width: 180},
                    {field: 'updateTime', title: '修改时间', width: 180},
                    {fixed: 'right', title:'操作', toolbar: '#toolBar', width:150}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            parseData: function(res){//将原始数据解析成 table 组件所规定的数据
                var result;
                if(this.page.curr){
                    result = res.data.slice(this.limit*(this.page.curr-1),this.limit*this.page.curr);
                }
                else{
                    result=res.data.slice(0,this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        })


        form.on('submit(data-search-btn)', function (data) {
            var categoryNames = demo1.getValue('valueStr');
            console.log('   ( ˘•ω•˘ )  分类是单选，将就用，总有一天给优化为多选 ')
            console.log(categoryNames);
            table.reload('currentArticleTable', {
                url: '/article/admin/queryArticle'
                , where: {
                    "articleTitle": data.field.articleTitle,
                    "articleCategory": categoryNames,
                    "articleAuthor": data.field.articleAuthor
                } //设定异步数据接口的额外参数
                //,height: 300
            });
        })

        //跳转至文章详情
        table.on('tool(articleTable)',function (obj){
            console.log('-----------')
            console.log(obj)
            var data = obj.data;
            if (obj.event === 'getArticleDetail'){
                window.open("/article/getArticleDetail/"+data.articleNum)
            }else if (obj.event == 'delete'){
                layer.confirm('真的删除行么', function (index) {
                    // obj.del();
                    console.log('111111111111111111111')
                    console.log(data)
                    $.ajax({
                        url: '/article/admin/deleteById',
                        data:{
                            'articleId' : data.id
                        },
                        type: 'post',
                        dataType: 'json',
                        success: function (res){
                            if (res.state == true){
                                layer.msg('删除成功',{icon:1,time:800},function (){
                                    layui.table.reload('currentArticleTable');
                                })
                            }else {
                                layer.msg('删除失败',{icon:2,time:800});
                            }
                        }
                    })


                    layer.close(index);
                });

            }
        })
    })
</script>
</html>
